﻿Public Class clsGtNgReasonDetails
    Public Function getGtNgDetailsList(b_gt_sys_id As String, procedure_no As String) As DataTable
        Dim szSql As String = ""
        szSql &= " select * from sf.b_gt_ng_reason_details "
        szSql &= "where b_gt_sys_id = ? and validity = ? and procedure_no = ?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, b_gt_sys_id, "1", procedure_no)
        If IsNothing(dt) Then
            getGtNgDetailsList = New DataTable
        Else
            getGtNgDetailsList = dt
        End If
    End Function

    Public Function save(en As entity_gt_ng_reason_details) As Integer
        Dim sql As String = ""
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            sql &= "INSERT INTO sf.b_gt_ng_reason_details(procedure_No, ng_num, ng_reason, operator, validity, b_gt_sys_id, update_time) "
            sql &= "VALUES ( ?, ?, ?, ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
            With en
                rtn = DBacs.ExecuteUpdateSql(sql, -1, .procedure_No, .ng_num, .ng_reason, .oper, .validity, .b_gt_sys_id)
            End With
            sql = ""
            sql &= "UPDATE sf.b_gt_result_details "
            sql &= " SET ok_num = ok_num - ?,ng_num = ng_num + ?, update_time=(SELECT CURRENT_TIMESTAMP(6))"
            sql &= " WHERE b_gt_sys_id = ? and procedure_no = ? "
            DBacs.ExecuteUpdateSql(sql, -1, en.ng_num, en.ng_num, en.b_gt_sys_id, en.procedure_No)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try

        Return rtn
    End Function

    Public Function updateValidity(sys_id As String, ng_num As String, b_gt_sys_id As String, procedure_no As String, oper As String) As Integer
        Dim sql As String = ""
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            Dim integer_procedure_No As Integer = Integer.Parse(procedure_no)

            sql &= "UPDATE sf.b_gt_ng_reason_details"
            sql &= " SET validity = ?, operator_validity = ?, update_time_validity = (SELECT CURRENT_TIMESTAMP(6)) "
            sql &= " WHERE sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(sql, -1, "0", oper, sys_id)

            sql = ""
            sql &= "UPDATE sf.b_gt_result_details"
            sql &= " SET ok_num = ok_num + ?, ng_num = ng_num - ?, update_time = (SELECT CURRENT_TIMESTAMP(6)) "
            sql &= " WHERE procedure_no = ? and b_gt_sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(sql, -1, ng_num, ng_num, procedure_no, b_gt_sys_id)

            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function
End Class
